<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html xmlns:th="" lang="zh-CN">
<%@include file="/WEB-INF/pages/base/include.jsp"%>
<body>
<div id="wrapper">
    <%@include file="/WEB-INF/pages/base/menu.jsp"%>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header">商品管理</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">商品列表</div>
                    <div class="panel-body inner-body">
                        <div class="main-box-search">
                            <form method="get" name="from1"  id="searchForm"  >
                                <div class="search-row">
                                    <div class="col-lg-3 col-md-6" style=" margin-bottom:15px; padding-left:0px;">
                                        <input class="form-control" type="text" name="productId" id="productId"
                                               placeholder="请输入商品ID">
                                    </div>
                                </div>
                                <div class="search-row">
                                    <div class="col-lg-3 col-md-6" style=" margin-bottom:15px; padding-left:0px;">
                                        <input class="form-control" type="text" name="name" id="name"
                                               placeholder="请输入商品名称">
                                    </div>
                                </div>
                                <a class="btn btn-primary" href="${webPath}toProductAdd" style="margin-left:15px;margin-bottom:30px;">添加商品</a>
                                <a class="btn btn-primary" onclick="deleteCache()" style="margin-left:15px;margin-bottom:30px;">更新商城首页缓存</a>
                            </form>
                        </div>
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
	                                <tr>
	                                	<th class="text-center" style="width:5%;">商品id</th>
	                                    <th class="text-center" style="width:5%;">商品首页序列</th>
	                                    <th class="text-center" style="width:10%;">商品图片</th>
	                                    <th class="text-center" style="width:16%">商品名称</th>
	                                    <th class="text-center" style="width:8%;">基本信息</th>
	                                    <th class="text-center" style="width:6%;">商品类型</th>
	                                    <th class="text-center" style="width:8%;">销售价格</th>
	                                    <th class="text-center" style="width:8%;">市场价格</th>
	                                    <th class="text-center" style="width:20%;">操作</th>
	                                </tr>
                                </thead>
                                <tbody id="productList">
                                
                                </tbody>
                            </table>
                        </div>
                        <div class="modal fade" id="delcfmModel">
                            <div class="modal-dialog modal-sm">
                                <div class="modal-content message_align">
                                    <div class="modal-header">
                                        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                        <h4 class="modal-title">提示信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <p>您确认要删除吗？</p>
                                    </div>
                                    <div class="modal-footer">
                                        <input id="url" type="hidden">
                                        <button class="btn btn-default" type="button" data-dismiss="modal">取消</button>
                                        <a class="btn btn-danger" onclick="pro_delete()" data-dismiss="modal">确定</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="modal fade" id="outModel">
                            <div class="modal-dialog modal-sm">
                                <div class="modal-content message_align">
                                    <div class="modal-header">
                                        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                        <h4 class="modal-title">提示信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <p>您确认要在承接页下架吗？</p>
                                    </div>
                                    <div class="modal-footer">
                                        <input id="url" type="hidden">
                                        <button class="btn btn-default" type="button" data-dismiss="modal">取消</button>
                                        <a class="btn btn-danger" onclick="pro_out()" data-dismiss="modal">确定</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal fade" id="addInModel">
                            <div class="modal-dialog modal-sm">
                                <div class="modal-content message_align">
                                    <div class="modal-header">
                                        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                        <h4 class="modal-title">提示信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <p>您确认要在承接页上传吗？</p>
                                    </div>
                                    <div class="modal-footer">
                                        <input id="url" type="hidden">
                                        <button class="btn btn-default" type="button" data-dismiss="modal">取消</button>
                                        <a class="btn btn-danger" onclick="pro_addIn()" data-dismiss="modal">确定</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- <div class="text-right">
                            <ul class="pagination" id="pagination">
                                <li><a href="javascript:void(0);" onclick="" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
				                <li><a  href="javascript:void(0);" onclick="" >1</a></li>
				                <li><a href="javascript:void(0);" onclick=""  aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
                            </ul>
                        </div> -->
                        <div class="box">
							<div id="pagination3" class="page fl"></div>
						</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:inline="javascript">
	searchCase(0,1);
	
	$('#searchForm').change(function(e) {
		searchCase(1,1);
	})
	
	var currentPage = 1;
	
	//搜索
	function searchCase(flag,page) {
		currentPage = page;
		
		var productId = null;
		var name = null;
		if(flag == 1){
			productId = $("#productId").val();
			name = $("#name").val();
		}
		var pageSize= 10;
		var baseURL = "${webPath}/product/list";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productId":productId,
	        	"name":name,
	            "page": page,
	            "pageSize": pageSize
	        }
	    }
	    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	        if (typeof result == 'string') result = JSON.parse(result);
	        if (result.body.result == '1') {
	            alert(result.body.description)
	            $("#productList").empty();
	        } else {
	        	$("#productList").empty();
	        	var list = result.body.list;
	        	console.log(list);
	        	var count = result.body.count;
	        	var str = "";
	        	if(list.length<=0){
	        		return
	        	}
	        	for(var i=0;i<list.length;i++){
	        		str += '<tr>';
	        		str += '	<td class="text-center">'+list[i].id+'</td>';
	        		str += '	<td class="text-center">'+list[i].orderNumber+'</td>';
	        		str += '	<td class="coverImg"><img class="center-block" src="'+list[i].image1+'"></td>';
	        		str += '	<td class="text-center">'+list[i].name+'</td>';
	        		str += '	<td>';
	        		str += '		供应商名称：'+list[i].productSellerName+'<br>';
	        		/*str += '		供应商名称： 商盟惠购商城<br>'
	        		str += '		商品二级分类：'+list[i].categoryName+'<br>'; */

	        		str += '	</td>';
	        		str += '	<td class="text-center">'+list[i].type+'</td>';
	        		str += '	<td class="text-center">'+list[i].salesPrice+'￥</td>';
	        		str += '	<td class="text-center">'+list[i].marketPrice+'￥</td>';
	        		/* str += '	<td class="text-center">';
	        		if(list[i].isUndertake == "1"){
	        			str += '		<a class="btn btn-default btn-delete" href="#" onclick=out("'+list[i].id+'")>下架</a>';
	        		}else{
	        			str += '		<a class="btn btn-default btn-delete" href="#" onclick=addIn("'+list[i].id+'")>上传</a>';
	        		}
	        		str += '	</td>'; */
	        		str += '	<td class="text-center">';
	        		str += '		<a class="btn btn-default" href="${webPath}toProductSummary/'+list[i].id+'">编辑商品规格</a>';
	        		str += '		<a class="btn btn-default" href="${webPath}toProductEdit/'+list[i].id+'">编辑</a>';
	        		//str += '		<a class="btn btn-default" href="${webPath}toProductFormat/'+list[i].id+'">规格</a>';
	        		str += '		<a class="btn btn-default btn-delete" href="#" onclick=delcfm("'+list[i].id+'")>删除</a>';
	        		str += '	</td>';
	        		str += '</tr>';
	        	}
	        	$("#productList").append(str);
	        	
	        	var currentPage = page;
	        	var sumPage = Math.ceil(count/pageSize);
	        	
	        	$("#pagination3").pagination({
					currentPage: currentPage,
					totalPage: sumPage,
					isShow: true,
					count: 10,
					homePageText: "首页",
					endPageText: "尾页",
					prevPageText: "上一页",
					nextPageText: "下一页",
					callback: function(current) {
						searchCase(1,current);
					}
				});
	        }
	    })
	}


	
	var productUuidCurrent = "";
	
    function delcfm(productUuid) {
        $('#delcfmModel').modal();
        productUuidCurrent = productUuid;
    }
    
    var idCurrent = "";
    function out(productId) {
        $('#outModel').modal();
        idCurrent = productId;
    }
    
    function addIn(productId) {
        $('#addInModel').modal();
        idCurrent = productId;
    }
    
    var pro_delete = function () {
    	var baseURL = "${webPath}/product/delete";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productId": productUuidCurrent
	        }
	    }
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('删除成功')
                searchCase(1,currentPage);
            } else {
                alert(result.body.description)
            }
        })
    }
    
    var deleteCache = function () {
   		var r=confirm("确定要更新首页缓存吗？")
  	  	if (r==true)
   	    {
	  	  	var baseURL = "http://smh.adxt.com/feichang-platform/cacheController/clearHomeIndexCache";
		    var json_package = {
		        "head": {},
		        "body": {
		        	"productId": productUuidCurrent
		        }
		    }
	        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	            if (typeof result == 'string') result = JSON.parse(result);
	            if (result.status == '1') {
	                alert('更新成功')
	            } else {
	                alert('更新失败');
	            }
	        })
   	    }
    }
    
    var pro_out = function () {
    	var baseURL = "${webPath}/product/inAndOut";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productId": idCurrent,
	        	"type": "0"
	        }
	    }
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('下架成功')
                searchCase(1,currentPage);
            } else {
                alert(result.body.description)
            }
        })
    }
    var pro_addIn = function () {
    	var baseURL = "${webPath}/product/inAndOut";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productId": idCurrent,
	        	"type": "1"
	        }
	    }
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('上传成功')
                searchCase(1,currentPage);
            } else {
                alert(result.body.description)
            }
        })
    }

    function upAndDown(deleteAt,productUuid) {
    	var baseURL = "${webPath}/product/upAndDown";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productUuid": productUuid,
	        	"deleteAt":deleteAt
	        }
	    }
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
            	alert(result.body.description)
            	searchCase(1,currentPage);
            } else {
                alert(result.body.description)
            }
        })
    }
    
    $(document).ready(function () {
        var lenth = $('#product_list tr').length;
        if (lenth == 0) {
            $('tbody#menu_list').append('<tr id="empty_menu"><td colspan="7" class="no-menu text-center">没有商品信息！</td></tr>');
        } else {
            $('#empty_menu').hide();
        }
    })

</script>
</body>
</html>
